<?php
$loader->requireOnce('includes/CommonConstant.class.php');

$loader->requireOnce('ordo/PatientTransferHistoryDetail.class.php');
$loader->requireOnce('includes/EnumerationConstant.class.php');
/**
 * Object Relational Persistence Mapping Class for table: patient_transfer_history
 *
 * @package	com.athena
 * @author	LHV
 */
class PatientTransferHistory extends ORDataObject {

	/**#@+
	 * Fields of table: patient_transfer_history mapped to class members
	 */
	var $patient_transfer_history_id		= '';
	var $ticket_id		= '';
	var $department_id		= '';
	var $start_time		= '';
	var $end_time		= '';
	var $patient_id      = '';
	var $treatment_duration_id		= '';
	var $status		= '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'patient_transfer_history';

	/**
	 * Primary Key
	 */
	var $_key = 'patient_transfer_history_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'PatientTransferHistory';

	/**
	 * Handle instantiation
	 */
	private $logger ;
	function __construct() {
		parent::ORDataObject();
		$this->logger = LoggerManager::getLogger('core.PatientTransferHistory');

	}



	/**
	 *
	 * @author hdviet
	 *
	 * @param $ticketId
	 * @param $departmentId
	 * @param $patientId
	 * @param $consTreatmentType
	 * @param $constAction
	 //check out department
	 *  CommonConstant::ACTION_CHECKOUT = "check_out";
	 //check in department
	 *  CommonConstant::ACTION_CHECKIN = "check_in";
	 //update patient status
	 *  CommonConstant::ACTION_RECORD_STATUS = "record_status";

	 * @param $keyEnumStatus
	 * @param $dateTime
	 * @return unknown_type
	 */
	public static function record($ticketId, $departmentId, $patientId, $consTreatmentType, $constAction, $keyEnumStatus, $dateTime) {
		$obj = Celini::newORDO("PatientTransferHistory");
		$obj->_record($ticketId, $departmentId, $patientId, $consTreatmentType, $constAction, $keyEnumStatus, $dateTime);
	}
	private function _record($ticketId, $departmentId, $patientId, $consTreatmentType, $constAction, $keyEnumStatus, $dateTime) {

		switch ($constAction) {
			case CommonConstant::ACTION_CHECKIN:
				$this->checkIn($ticketId, $departmentId, $patientId, $consTreatmentType,$keyEnumStatus, $dateTime);
				break;
			case CommonConstant::ACTION_CHECKOUT:
				$this->checkOut($ticketId, $departmentId, $patientId, $consTreatmentType, $keyEnumStatus, $dateTime);
				break;
			case CommonConstant::ACTION_RECORD_STATUS:
				$this->recordStatus($ticketId, $departmentId, $patientId, $consTreatmentType, $keyEnumStatus, $dateTime);
                break;
			default:
				;
				break;
		}

	}
	private function recordStatus($ticketId, $departmentId, $patientId, $consTreatmentType, $keyEnumStatus, $dateTime) {
	   $arrPH = $this->getPatientTransferHistory($ticketId);
        
        //create new record in detail table
        $detailObj = Celini::newORDO("PatientTransferHistoryDetail");        
        $detailData = array (
            'patient_transfer_history_id'=> $arrPH['patient_transfer_history_id'],
            'in'=>$consTreatmentType,
            'date_time'=>$dateTime,
            'enum_status' => $keyEnumStatus
         );
         
         return $detailObj->addNew($detailData);         		
	}

	private function checkOut($ticketId, $departmentId, $patientId, $consTreatmentType, $keyEnumStatus, $dateTime) {

        $arrPH = $this->getPatientTransferHistory($ticketId);
        if ($arrPH == null) {
            $this->set_id('');
        } else {

            $this->setup($arrPH['patient_transfer_history_id']);
        }

        $data = array('ticket_id'=>$ticketId,
                      'department_id'=>$departmentId,
                      'end_time'=>$dateTime,
                      'patient_id'=>$patientId,
                      'status'=>CommonConstant::CLOSE);
        $this->populateArray($data);
        $this->persist();
        $id = $this->get_id();

        //create new record in detail table
        $detailObj = Celini::newORDO("PatientTransferHistoryDetail");
        $hisDetail = $detailObj->getHisDetail($ticketId, $keyEnumStatus);
        $detailData = array (
            'patient_transfer_history_id'=> $id,
            'in'=>$consTreatmentType,
            'date_time'=>$dateTime,
            'enum_status' => $keyEnumStatus
            );
        if ($hisDetail) {
        	//update if exist
        	$detailObj->setup($hisDetail['patient_transfer_history_detail_id']);
            $detailObj->populateArray($detailData);
            $detailObj->persist();

        } else {
        	//add new if not exist
        	$detailObj->addNew($detailData);
        }

        return $id;
    }


	private function checkIn($ticketId, $departmentId, $patientId, $consTreatmentType, $keyEnumStatus, $dateTime) {

		$arrPH = $this->getPatientTransferHistory($ticketId);
		if ($arrPH == null) {
			$this->set_id('');
		} else {

			return $arrPH['patient_transfer_history_id'];
		}

		$data = array('ticket_id'=>$ticketId,
                      'department_id'=>$departmentId,
                      'start_time'=>$dateTime,
                      'patient_id'=>$patientId,
                      'status'=>CommonConstant::OPEN);

		$this->populateArray($data);
		$this->persist();
		$id = $this->get_id();

		//create new record in detail table
		$detailObj = Celini::newORDO("PatientTransferHistoryDetail");
		if (!$detailObj->getHisDetail($ticketId, $keyEnumStatus)) {
			$detailData = array (
       	    'patient_transfer_history_id'=> $id,
       	    'in'=>$consTreatmentType,
       	    'date_time'=>$dateTime,
			'enum_status' => $keyEnumStatus
			);

			$detailObj->addNew($detailData);
		}

		return $id;
	}

	public function getPatientTransferHistory($ticketId) {
		$ds = new Datasource_sql();
		$sql = array(
            'cols'  => "patient_transfer_history_id, department_id, start_time, end_time, status",
            'from'  => " $this->_table ",
            'where' => "ticket_id = '" . $ticketId . "'"
            );

            $ds->setup($this->_db, $sql);
            $result = $ds->toArray();
            if (empty($result)) {
            	return null;
            }
            return $result[0];

	}

	public function getObjectByTicketId($ticketId) {
          $sql  =   "SELECT $this->_key FROM $this->_table WHERE ticket_id='$ticketId'";
          $res  =   $this->_execute($sql);
          $obj  =   Celini::newORDO('PatientTransferHistory', $res->fields["$this->_key"]);

          return $obj;
	}






}
?>
